<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>刷数据</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('/layuiadmin/style/admin.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/formSelects-v4.css')}}" media="all">
    <style>
        .userbox,.storebox{height:190px;margin-right:0; overflow-y: auto; z-index: 999; position: absolute; left: 0px; top: 63px; width:298px; background-color:#ffffff; border: 1px solid #ddd;}
        .userbox .list,.storebox .list{height:38px;line-height: 38px;cursor:pointer; padding-left:10px;}
        .userbox .list:hover,.storebox .list:hover{background-color:#eeeeee;}
        .yname{font-size: 13px; color: #444; margin-bottom:8px;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card" style="margin-top:0px">
        <div class="layui-card-header">刷数据</div>
        <div class="layui-card-body" style="padding: 15px;min-height: calc(100vh - 100px)">
            <div class="layui-form" lay-filter="component-form-group" style="margin-right:0;display: inline-block;">
                <div class="layui-input-block" style="color: red;margin-left:0px;font-size: 16px;">
                    每天晚上十点后才可刷数据！
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width:450px;margin-right:0;">
                            <text class="yname">选择门店</text>
                            <select name="storeNameSel" id="storeNameSel" xm-select="storeNameSel" xm-select-search="">
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width:250px;margin-right:0;">
                            <text class="yname">开始时间</text>
                            <input type="text" style="border-radius:5px" class="layui-input start-item test-item" placeholder="订单开始时间" lay-key="23">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width:250px;margin-right:0;">
                            <text class="yname">结束时间</text>
                            <input type="text" style="border-radius:5px" class="layui-input end-item test-item" placeholder="订单结束时间" lay-key="24">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn submit" style="border-radius:5px;margin-top:20px;margin-bottom: 0;height:36px;line-height: 36px;">
                            确定
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<input type="hidden" class="js_user_id">

<script src="{{asset('/layuiadmin/layui/layui.js')}}"></script>
<script>
    var token = localStorage.getItem("Usertoken");

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        formSelects: 'formSelects'
    }).use(['index','form', 'table','laydate','formSelects'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,form = layui.form
            ,laydate = layui.laydate
            ,formSelects = layui.formSelects;

        // 未登录,跳转登录页面
        $(document).ready(function(){
            if(token==null){
                window.location.href="{{url('/user/login')}}";
            }
        });

        formSelects.config('storeNameSel', {
            type: 'get',
            searchUrl: "{{url('/api/user/store_lists?token=')}}"+token,
            searchName: 'store_name', // 自定义搜索内容的 key 值
            delay: 500, // 搜索延迟时间，默认停止输入 500ms 后开始搜索
            beforeSuccess: function(id, url, searchVal, result){
                let arr = [];
                //我要把数据外层的code, msg, data去掉
                result = result.data;
                for(var i=0;i<result.length;i++){
                    var data ={"value":result[i].store_id,"name":result[i].store_name};
                    arr.push(data);
                    // console.log(arr);
                }
                console.log(arr);
                //然后返回数据
                return arr;
            },
            beforeSearch: function (id, url, searchVal) {
                if (!searchVal) {
                    // 如果搜索内容为空，就不触发搜索
                    return false
                }
                return true
            },
        }).data('storeNameSel', 'server', {
            url:"{{url('/api/user/store_lists?token=')}}"+token+"&l=1000",
        })

        //转移数据-选择门店
        var storeSelData = []
        formSelects.on('storeNameSel', function (id, vals, val, isAdd, isDisabled) {
            console.log(1, '当前选中 id', id)
            console.log(2, '已选中值', vals)
            console.log(3, '点击值', val)
            console.log(4, '当前操作选中或取消', isAdd)
            console.log(5, '当前项是否是 disabled', isDisabled)

            if(isAdd==true){
                if(!storeSelData.includes(val.value)){
                    storeSelData.push(val.value);
                }
            }else{
                if(storeSelData.includes(val.value)){
                    storeSelData = storeSelData.filter(item => item !== val.value);
                }
            }
            console.log(storeSelData,'storeSelData-----')
        })

        laydate.render({
            elem: '.start-item'
            ,type: 'datetime'
            ,done: function(value){
                //执行重载
                // table.reload('test-table-page', {
                //     where: {
                //         time_start:value
                //         ,time_end:$('.end-item').val()
                //     }
                //     ,page: {
                //         curr: 1 //重新从第 1 页开始
                //     }
                // });
            }
        });

        laydate.render({
            elem: '.end-item'
            ,type: 'datetime'
            ,ready: function(date){
                this.dateTime.hours=23;
                this.dateTime.minutes=59;
                this.dateTime.seconds=59;
            }
            ,done: function(value){
                //执行重载
                // table.reload('test-table-page', {
                //     where: {
                //         time_start:$('.start-item').val()
                //         ,time_end:value
                //     }
                //     ,page: {
                //         curr: 1 //重新从第 1 页开始
                //     }
                // });
            }
        });

        var pageLoading;
        $('.submit').on('click', function(){
            if(!storeSelData || storeSelData.length==0){
                layer.msg('请先选择门店！')
                return false;
            }
            let starttime = $('.start-item').val();
            let endtime = $('.end-item').val();
            if(!starttime || !endtime){
                layer.msg('开始时间和结束时间是必须选择的！')
                return false;
            }
            // 转换为 Date 对象
            let startDate = Date.parse(starttime);
            let endDate = Date.parse(endtime);
            console.log(startDate,endDate)
            if(startDate > endDate){
                layer.msg('开始时间不能大于结束时间');
                return false;
            }
            // 验证不能跨月
            if(new Date(startDate).getFullYear() !== new Date(endDate).getFullYear() || new Date(startDate).getMonth() !== new Date(endDate).getMonth()){
                layer.msg('时间范围不能跨月');
                return false;
            }
            // 判断是否大于等于晚上10点
            var currentHour = new Date().getHours();
            if(currentHour < 22){
                layer.msg('每天晚上十点后才可刷数据');
                return false;
            }
            pageLoading = layer.msg('数据处理中..', {
                icon: 16
                ,shade: 0.3
                ,time: false
            });
            $.post("{{url('/api/v2/system/data/dataSave')}}",
                {
                    token:token,
                    store_id:storeSelData,
                    start_time:starttime,
                    end_time:endtime
                },function(res){
                    if(res.status==1){
                        layer.close(pageLoading);
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 1
                            ,time: 3000
                        });
                    }else{
                        layer.close(pageLoading);
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 1
                            ,time: 3000
                        });
                    }
                },"json");
        });

    });

</script>

</body>
</html>
